// pages/order/takeMeal/index.js
const App=getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    img: App.Imges.food,
    out: App.Imges.out,
    start:false,

    ispay: false,
    // status: 3,  // 订单状态 1 待付款 2 已付款 3 已完成
    id: '',
    isReser: false, // false 下单的  true预约的
    order: {},
    time: '',

    list:[]
  },
  out(){
    this.setData({
      start:false
    })
  },
  cancel(e){
    this.setData({
      start: true
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      id: options.id,
      isReser: options.isReser
    })
  },
  onShow() {
    this.getList();
    this.getOrder();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getList();
    this.getOrder();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  getOrder() {
    let url = 'mp/dishes/like';
    App.ajax(url, 'GET', '').then(res => {
      if (res.status === 10000) {
        this.setData({
          list: res.data
        })
      }
    }).catch(err => {
      console.log(err)
    })
  },
  // 请求
  getList() {
    let url = 'mp/order/' + this.data.id;
    App.ajax(url, 'GET').then(res => {
      console.log(res)
      if (res.status === 10000) {
        this.setData({
          order: res.data
        })
        // 如果是未支付订单，这里检测时间
        if (res.data.status === 1) {
          let thisTime = new Date().getTime();
          let time = new Date(res.data.order_time).getTime();
          // let numberTime = time + (1000 * 60 * 13);
          if (time > thisTime) {
            // if (thisTime > numberTime) {
            this.setData({
              time: this.timeFun(time - thisTime)
              // time: this.timeFun(thisTime - numberTime)
            });
            this.setTimeInner(time - thisTime);
            // this.setTimeInner(thisTime - numberTime);
          }
        }
      } else {
        App.prompts(res.msg);
      }
    }).catch(err => {
      console.log(err)
    })
  },

  // 自定义方法
  setTimeInner (val) {
    if (val > 0) {
      
    }
  },
  timeFun(time) {
    let str = '';
    let f = Math.floor(time / (1000 * 60));
    let leave3 = time % (60 * 1000)      //计算分钟数后剩余的毫秒数
    let seconds = Math.round(leave3 / 1000)
    // let m = Math.floor(time / (1000 * 60 * 60));
    if (f > 0) {
      str = f + ' : ';
    } else {
      str = '00 : ';
    }
    // if (m > 0) {
    //   str = str + m;
    // } else {
    //   str = str + '00'
    // }
    if (seconds > 0) {
      str = str + seconds;
    } else {
      str = str + '00'
    }
    console.log('回传', str)
    return str;
  },
  goAppraise (e) {
    console.log('去评价', e);
    let item = e.currentTarget.dataset.item;
    let order = {
      order_id: item.order_dishes.order_id,
      canteen_id: item.order_dishes.dishes_id,
      name: item.name,
      log: item.img_src,
      center: item.recommend,
      counts: item.order_dishes.quantity,
      price: item.price,
    };
    // 存菜品信息
    wx.setStorage({
      key:"canteenInfo",
      data: order
    });
    wx.navigateTo({
      url: '/pages/order/appraise/index'
    })
  },
  goUrl(e) {
    let url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    })
  },
  goPay() {
    this.setData({
      ispay: this.data.ispay ? false : true
    })
  }
})